<?php
/**
 *author:韩帅
 *createtime:2017/6/20 10:58
 *description:
 */
namespace App\Http\Controllers\App;

use App\Models\Card;
use App\Models\User;
use Illuminate\Http\Request;
use App\Models\CardType;
use App\Http\Controllers\Controller;
use App\Http\Controllers\App\BaseController;

class CardController extends  BaseController
{
    /**
     * 银行卡列表
     */

    public function cardList()
    {
        $return = [];
        $userId = session('user_id');

        $data   = Card::where('user_id', $userId)->where('status', Card::STATUS_BEGIN)
                                                 ->with('cardType')
                                                 ->get()
                                                 ->toArray();

        $return = $data;

        if (empty($return)) {

            $return['card_list'] = [];
            return self::json($return, '数据为空', 200);
        }

        //将数据处理一下给APP
        $newArr = Card::cardList($return);

        return self::json($newArr, '成功', 200);
    }

    /**
     * 银行卡类型列表
     */
    public function cardTypeList()
    {
        $return = [];    //存放查出的数据
        $newArr = [];    //存放处理后的数据

        $user_id= session('user_id');
        $info   = User::find($user_id);

        if (empty($info)) {
            return self::error('当前登录用户异常');
        }

        if (empty($info['real_name']) || empty($info['id_card'])) {
            return self::error('请先实名认证');
        }

        $data   = CardType::where('status',CardType::STATUS_BEGIN)->select('id','name')->get()->toArray();

        $return = $data;

        if (empty($return)) {
            return self::json($return,'数据为空',400);
        }

        $newArr['cardTypeList'] = $return;
        $newArr['real_name']    = $info['real_name'];
        $newArr['id_card']      = $info['id_card'];

        return self::json($newArr,'成功',200);
    }

    /**
     * 银行卡添加
     */
    public function cardAdd(Request $request)
    {

        $userId    = session('user_id');
        $card_num  = trim($request->input('cardNum',''));
        $card_type = trim($request->input('cardType',''));

        if (empty($userId) || empty($card_num) || empty($card_type)) {
            return self::error('必填字段不能为空');
        }

        $res = Card::check_card($card_num);

        if ($res === false) {
            return self::error('银行卡号格式有误');
        }

        $res = $this->cardCheck($userId,$card_num);

        if ($res) {
            return self::error('不能重复添加');
        }

        $obj    = new Card();

        $obj->card_num  = $card_num;
        $obj->user_id   = $userId;
        $obj->card_type = $card_type;

        $res = $obj->save();

        if ($res == false) {
            return self::error('数据为空');
        }
        return self::success('添加成功');
    }

    /**
     * 银行卡删除
     */
    public function cardDelete(Request $request)
    {

        $id = trim($request->input('id',''));

        if (empty($id)) {
            return self::error('ID不能为空');
        }

        $obj  = Card::find($id);

        if (empty($obj)) {
            return self::error('记录不存在');
        }

        $obj->status = Card::ISDELETE_NO;
        $res = $obj->save();

        if ($res == false) {
            return self::error('删除失败');
        }
        return self::success('删除成功');
    }

    /**
     *
     */

    /**
     *验证是否重复
     */
    public function cardCheck($user_id,$cardNum) {

        $obj = new Card();

        $data = $obj->where('user_id',$user_id)->where('status',Card::STATUS_BEGIN)->where('card_num',$cardNum)->first();

        if (!empty($data)) {
            return true;
        }
        return false;
    }
}